import {
  updatePortrait
} from '@/api/user.js'
export default {
  data() {
    return {
      configs: {
        action: process.env.VUE_APP_BASE_API + '/course/file/upload',
        name: 'file',
        autoupload: true,
        multiple: false,
        disabled: false,
        limit: 1
      },
      filelist: [],
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      formData: {
        portraitUrl: ''
      }
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.formData.portraitUrl = res.data
      this.imageUrl = URL.createObjectURL(file.raw)
      // location.reload()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    beforeAvatarUpload(file) {
      console.log(file)
      if (this.fileList.length > 1) {
        this.$message.error('只能上传一张头像图片!')
        return false
      }
      const file_type = ['image/jpg', 'image/jpeg', 'image/png', 'image/x-png']
      const isJPG = file_type.indexOf(file.type) > -1
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    uploadSubmit() {
      console.log(this.formData)
      updatePortrait(this.formData).then(response => {
        if (response.code === 200 || response.code === 20000) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$emit('backUploaderAvatar', true)
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
          this.$emit('backUploaderAvatar', false)
        }
      })
    }
  }
}
